//
//  RadioViewController.m
//  MusicPlayer
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RadioViewController.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "RadioModel.h"
#import "RadioCollectionReusableView.h"
#import "RadioCollectionViewCell.h"
#import "RadioDetailViewController.h"
@interface RadioViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *typeTable;
@property (nonatomic, retain) NSMutableArray *array;
@end

@implementation RadioViewController
- (void)dealloc
{
    [_array release];
    [_typeTable release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
      

    }
    return self;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor clearColor]];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor clearColor]];
  
    [SANetWorkingTask request:@"http://fm.api.ttpod.com/radiolist?image_type=240_200" parmater:nil block:^(NSDictionary *result) {
        
        if (result.count > 0) {
            
            NSArray *array = [result objectForKey:@"data"];
            for (NSDictionary *dic in array) {
                RadioModel *radioModel = [[RadioModel alloc] init];
                [radioModel setValuesForKeysWithDictionary:dic];
                [self.array addObject:radioModel];
        }
        }
      
        [_typeTable reloadData];
        
    }];

    
    [self creatview];
}


- (void)creatview
{
    
    UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc] init];
    layOut.minimumLineSpacing = self.view.frame.size.height/667.0* 20;
    layOut.minimumInteritemSpacing = self.view.frame.size.width/375*   20;
    layOut.sectionInset = UIEdgeInsetsMake( self.view.frame.size.width/375* 15, self.view.frame.size.height/667.0*15, self.view.frame.size.width/375*15,self.view.frame.size.height/667.0* 15);
    layOut.itemSize = CGSizeMake(self.view.frame.size.width/375*100,self.view.frame.size.height/667.0* 130);
    layOut.headerReferenceSize = CGSizeMake(0, self.view.frame.size.height/667.0*20);
    
   
    
    self.typeTable = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height/667.0*200) collectionViewLayout:layOut];
    self.typeTable.dataSource = self;
    self.typeTable.delegate = self;
    self.typeTable.backgroundColor = [UIColor clearColor];
    [self.typeTable registerClass:[RadioCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.typeTable registerClass:[RadioCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_typeTable];
    [layOut release];
    [self.typeTable release];
 
}

// 确定section个数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.array.count;
    
}



//cell的视图
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     RadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
    RadioModel *radio = [self.array objectAtIndex:indexPath.section];
    
    NSArray *arr = radio.data;
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"pic_url_240_200"]]];
    cell.label.text = [dic objectForKey:@"tag_name"];
    
    return cell;
}


// 每个section中item的个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    RadioModel *model = [self.array objectAtIndex:section];
    NSArray *array = model.data;
    return array.count;
}


#pragma mark -
#pragma mark section标题
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
     RadioCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];

    
    RadioModel *radio = [self.array objectAtIndex:indexPath.section];

    header.titleLabel.text = radio.tag_type_name;
 
    
    header.backgroundColor = [UIColor clearColor];
  
    return header;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
  
    
    RadioModel *radio = [self.array objectAtIndex:indexPath.section];
    NSArray *arr = radio.data;
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    
    NSString *num = [NSString stringWithFormat:@"%@", [dic objectForKey:@"tag_id"]];
    
    
    if ( [num isEqualToString:@"8"]) {
    
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该电台尚无新歌" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [al show];
       
    } else {
    
    NSString *website = [NSString stringWithFormat:@"http://fm.api.ttpod.com/vipradiosong?userid=0000001&num=150&tagid=%@", [dic objectForKey:@"tag_id"]];
    
    RadioDetailViewController *second = [[RadioDetailViewController alloc] init];
     
    second.webId = website;
    second.pic = [dic objectForKey:@"large_pic_url"];
    second.title = [dic objectForKey:@"tag_name"];
    [self.navigationController pushViewController:second animated:YES]
    ;
    [second release];
    }
}



@end
